﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Resources;
using Microsoft.Phone.Controls;
using Newtonsoft.Json.Linq;
//using TheFSharp;
//using MobileOA;

namespace MobileOA
{
    public partial class TodoList : PhoneApplicationPage
    {
        public TodoList()
        {
            InitializeComponent();
            //AddItems();
            //readFile();
            Debug.WriteLine("Initialize");
            DoHttpWebRequest();
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
        }

        /*
        private void AddItems()
        {
            for (int i = 0; i < 50; i++)
            {
                ListBoxItem item = new ListBoxItem();
                TextBlock textBlock = new TextBlock();
                textBlock.Text = i.ToString();
                item.Content = textBlock;
                listBox1.Items.Add(item);
            }
        }
        */

        private void readFile()
        {
            string fileName = "/MobileOA;component/TodoList.json";
            StreamResourceInfo resourceInfo = Application.GetResourceStream(new Uri(fileName, UriKind.Relative));

            using (var reader = new StreamReader(resourceInfo.Stream))
            {
                //textBlock1.Text = reader.ReadToEnd();
                //reader.Close();
            }
        }

        private void DoHttpWebRequest()
        {
            //string url = "http://www.cnblogs.com/linzheng";
            //string url = "http://localhost:8080/MobileOA/TodoList?username=lihua&password=pass";
            string url = "http://localhost:8080/MobileOA/TodoList?username=admin&password=pass";
            WebRequest request = WebRequest.Create(url); //创建WebRequest类
            IAsyncResult result = request.BeginGetResponse(ResponseCallback, request); //返回异步操作的状态
        }

        private void ResponseCallback(IAsyncResult result)
        {
            var request = (HttpWebRequest) result.AsyncState; //获取异步操作返回的的信息
            WebResponse response = request.EndGetResponse(result); //结束对 Internet 资源的异步请求
            using (Stream stream = response.GetResponseStream())
            using (var reader = new StreamReader(stream))
            {
                string contents = reader.ReadToEnd();
                //Dispatcher.BeginInvoke(delegate() { this.textBlock1.Text = contents; });
                Dispatcher.BeginInvoke(delegate { ParseJson(contents); });
            }
        }

        private void ParseJson(string jsonString)
        {
            Debug.WriteLine(jsonString);
            //JObject json = JObject.Parse(jsonString);
            //*
            JArray jArray = JArray.Parse(jsonString);
            Debug.WriteLine(jArray.Count.ToString());
            for (int i = 0; i < jArray.Count; i++)
            {
                var obj = (JObject) jArray[i];
                if (null != obj["Subject"])
                {
                    var item = new ListBoxItem();
                    var textBlock = new TextBlock();
                    textBlock.Text = obj["Subject"].ToString();
                    item.Content = textBlock;
                    listBox1.Items.Add(item);
                }
            }

//            TheFSharp.Square(10);

//            Tuple<int, int> tuple = Class1.Swap(10, 9);
//            Class1.Add(1, 2);
            //*/
            /*
            JObject obj;
            StringReader stringReader = new StringReader(json);
            JsonReader reader = new JsonReader(stringReader);
 
            //Console.WriteLine("TokenType\t\tValueType\t\tValue");
 
            while (reader.Read())
            {
                 //Console.WriteLine(reader.TokenType + "\t\t" + WriteValue(reader.ValueType) + "\t\t" + WriteValue(reader.Value))
            }
            Newtonsoft.Json.Converters.
            JsonReader reader = new JsonReader(new StringReader(jsonText));
            //JObject json = JObject.Parse(json);
            JavaScriptArray javascript = (JavaScriptArray)JavaScriptConvert.DeserializeObject(json);

            for (int i = 0; i < javascript.Count; i++)
            {
                JavaScriptObject obj = (JavaScriptObject)javascript[i];
                sb.Append("ID：" + obj["ID"].ToString());
                sb.Append("Name：" + obj["Name"].ToString());
                JavaScriptArray json = (JavaScriptArray)obj["Other"];

                for (int j = 0; j < json.Count; j++)
                {
                    JavaScriptObject jsonobj = (JavaScriptObject)json[j];
                    sb.Append("Age：" + jsonobj["Age"].ToString());
                    sb.Append("Sex：" + jsonobj["Sex"].ToString());
                }
            }  */
        }
    }
}